import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    searchlastrouter: "",
    classifylastrouter: "",
    classifysearch: "",
    favList: [],
    login: false,
    loginstate: null,
    user: [{
      name: "fujialuo",
      password: "123456"
    }]
  },
  mutations: {
    savesearchrouter(state, str) {
      state.searchlastrouter = str
    },
    saveclassifyrouter(state, str) {
      state.classifylastrouter = str
    },
    classifytosearch(state, str) {
      state.classifysearch = str
    },
    initFav(state) {
      if (JSON.parse(localStorage.getItem('fav'))) {
        state.favList = JSON.parse(localStorage.getItem('fav'));
      }
    },
    addfav(state, id) {
      state.favList.push(id);
      localStorage.setItem("fav", JSON.stringify(state.favList));
    },
    cutfav(state, id) {
      let n = state.favList.findIndex(ele => ele == id);
      state.favList.splice(n - 1, 1);
      localStorage.setItem("fav", JSON.stringify(state.favList));
    },
    tologin(state, [name, password]) {
      state.loginstate = null;
      console.log(name, password);
      let n = state.user.find(ele => ele.name == name);
      console.log(n);
      if (n == undefined || n.password != password) {
        state.loginstate = false;
      } else {
        window.localStorage.setItem("login", true);
        state.loginstate = true;
      }
    },
  },
  actions: {},
  modules: {}
})